/*
 * @描述: 新消息通知页面逻辑
 * @版权所有: 广东国星科技有限公司
 * @商业授权: www.mscodecloud.com
 */
import React, { Component, Fragment } from 'react';
import { formatMessage } from 'umi-plugin-react/locale';
import { Switch, List, message } from 'antd';
import { connect } from 'dva';

@connect(({ user }) => ({
  currentUser: user.currentUser
}))
class NotificationView extends Component {
  onChange(checked, field) {
    const { currentUser, dispatch } = this.props;
    dispatch({
      type: 'account/updateSysUserInfo',
      payload: {
        fieldValue: checked ? 1 : 0,
        field: field,
        id: currentUser.id
      }
    }).then(response => {
      if (response.message != 'success') {
        message.warning(response.message);
      }
      dispatch({
        type: 'user/fetchCurrent'
      });
    });
  }

  getData = () => {
    const { currentUser } = this.props;
    return [
      {
        title: formatMessage({ id: 'app.settings.notification.messages' }, {}),
        description: formatMessage({ id: 'app.settings.notification.messages-description' }, {}),
        actions: [<Switch
          checkedChildren={formatMessage({ id: 'app.settings.open' })}
          unCheckedChildren={formatMessage({ id: 'app.settings.close' })}
          defaultChecked={currentUser.notifyMessage === 1 ? true : false}
          onChange={(value) => { this.onChange(value, 'notifyMessage') }}
        />]
      },
      {
        title: formatMessage({ id: 'app.settings.notification.todo' }, {}),
        description: formatMessage({ id: 'app.settings.notification.todo-description' }, {}),
        actions: [<Switch
          checkedChildren={formatMessage({ id: 'app.settings.open' })}
          unCheckedChildren={formatMessage({ id: 'app.settings.close' })}
          defaultChecked={currentUser.notifyTodo === 1 ? true : false}
          onChange={(value) => { this.onChange(value, 'notifyTodo') }}
        />]
      }
    ];
  };

  render() {
    return (
      <Fragment>
        <List
          itemLayout="horizontal"
          dataSource={this.getData()}
          renderItem={item => (
            <List.Item actions={item.actions}>
              <List.Item.Meta title={item.title} description={item.description} />
            </List.Item>
          )}
        />
      </Fragment>
    );
  }
}

export default NotificationView;
